<?php

/**
 * 
 * @author chongi
 *
 *
 *
 * 
 * @method check() 检测请求参数，调用权限
 * 
 * @method auth() 验证签名
 * 
 * @method call()  调用业务类
 * 
 */

/**
 * 数据签名
 * @param array 	$params			签名数据
 * @param string	$secertKey		签名私钥
 * @return string
 * 
 */

class apiServiceHandler {
	
	
	//访问权限
	private $permission=array(
			'a4a6cb8b60695d718a902afaba4c2765' => array(					//公钥
					'secretKey' => '187a5c1ae5501d1e7a1df907365a66a9',		//用于签名私
					'useFlg'	=> 1, 										//是否允许使用
					'scope'		=> array(
									'xshop' => 1,										//业务功能类名
					),

			)
	);
	
	//私钥
	private $secertKey = '';
	
	private $params=array();
	
	
	/**
	 * 检测参数以及权限
	 * 
	 * @return boolean
	 */
	public function check(){
		
		if (! isset($_REQUEST['api']) &&  empty($_REQUEST['api'])) {
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'api','data' => '')));
		}
		
		if (! isset($_REQUEST['sign']) &&  empty($_REQUEST['sign'])) {
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'sign','data' => '')));
		}
		
	
		if (! isset($_REQUEST['scope']) &&  empty($_REQUEST['scope'])) {
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'scope','data' => '')));
		}
		

		if (! isset($_REQUEST['ver']) &&  empty($_REQUEST['ver'])) {
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'ver','data' => '')));
		}
		

		if (! isset($_REQUEST['param']) &&  empty($_REQUEST['param'])) {
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'api','data' => '')));
		}
		
		if (! isset($_REQUEST['req']) ) {
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => '缺少必须的参数' . 'req','data' => '')));
		}
		
		
		$apiKey = $_REQUEST['api'];
		
		if ( ! isset($this->permission[$apiKey])){
			header("Content-type: application/json");
			die(json_encode(array('code' => 400,'message' => 'api非法','data' => '')));
		}
		
		if ($this->permission[$apiKey]['useFlg'] == 0){
			header("Content-type: application/json");
			die(json_encode(array('code' => 403,'message' => '非法访问','data' => '')));
		}
		
		if ($this->permission[$apiKey]['scope'] == 0){
			header("Content-type: application/json");
			die(json_encode(array('code' => 403,'message' => 'scope访问非法','data' => '')));
		}
		
		$this->secertKey = $this->permission[$apiKey]['secretKey'];
		
		
	}
	
	/**
	 * 验证签名
	 * 
	 */
	public function auth(){
		$clientSign=$_REQUEST['sign'];
		$params=$_REQUEST;
		unset($params['sign'], $params['scope']);
		ksort($params);
		$unsigned_string = '';
		foreach ($params as $key => $val) {
			$unsigned_string .= $key . $val;
		}
		$serverSign=md5($unsigned_string . $this->secertKey);
		
		if ($clientSign != $serverSign){
			header("Content-type: application/json");
			die(json_encode(array('code' => 403,'message' => '签名错误','data' => '')));
		}
		
		$this->params=json_decode($_REQUEST['param'],true);
	}
	
	
	/**
	 * 调用业务类
	 */
	public function call(){
		
	
		
		$xShopHandler = new xShopHandler ;
		$resultArray  = $xShopHandler->callMQ($this->params) ;
		
		header("Content-type: application/json");
		echo json_encode( array(
				'data' 	  => $resultArray['data'],		//业务类返回数
				'code' 	  => $resultArray['code'],	    //返回状态码
				'message' => $resultArray['message'],	//返回提示信息
 		));
		die();
	}
	
	
}